<?php

class m_perusahaan extends CI_Model {

    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

    function get_count_all_perusahaan() {
        $sql = "SELECT COUNT(perusahaan_id)'total' FROM perusahaan";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result['total'];
        } else {
            return array();
        }
    } 

    // get user profil
    function get_all_perusahaan() {
        $sql = "SELECT * FROM perusahaan ORDER BY perusahaan_nama";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    } 
    
    // get user profil
    function get_all_perusahaan_pagination($params) {
        $sql = "SELECT * FROM perusahaan ORDER BY perusahaan_nama LIMIT ?, ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->result_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    } 

    function get_perusahaan_by_id($params) {
        $sql = "SELECT * FROM perusahaan WHERE perusahaan_id = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
            return $result;
        } else {
            return array();
        }
    }
    
    function is_exist_nama($params) {
        $sql = "SELECT * FROM perusahaan WHERE perusahaan_nama = ?";
        $query = $this->db->query($sql, $params);
        if ($query->num_rows() > 0) {
            $query->free_result();
            return true;
        } else {
            return false;
        }
    } 

    // insert
    function insert($user_id) {
        // update by this date
        $sql = "INSERT INTO perusahaan(perusahaan_nama, perusahaan_logo, mdb, mdd) VALUES (?, ?, ?, NOW())";
        return $this->db->query($sql, $user_id);
    }

    // edit
    function edit($user_id) {
        // update by this date
        $sql = "UPDATE perusahaan SET perusahaan_nama = ?, perusahaan_logo = ?, mdb = ? WHERE perusahaan_id = ?";
        return $this->db->query($sql, $user_id);
    }

    // delete
    function delete($user_id) {
        // update by this date
        $sql = "DELETE FROM perusahaan WHERE perusahaan_id = ?";
        return $this->db->query($sql, $user_id);
    }

} 